package com.zyc.dao;

import com.zyc.bean.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("studentDao")
public class StudentDaoImpl implements StudentDao {

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Override
    public int addStudent(Student student) {
        String sql = "INSERT INTO students (number, name, grade, clbum, gender) VALUES (?, ?, ?, ?, ?)";
        int i = jdbcTemplate.update(sql,student.getNumber(),student.getName(),student.getGrade(),student.getClbum(),student.getGender());
        return i;
    }

    @Override
    public int updateSudent(Student student) {
        String sql = "UPDATE students SET name = ?, grade = ?, clbum = ?, gender = ? WHERE number = ?";
        int i = jdbcTemplate.update(sql,
                student.getName(),
                student.getGrade(),
                student.getClbum(),
                student.getGender(),
                student.getNumber());
        return i;
    }

    @Override
    public Student selectStudent(String number) {
        String sql = "select * from students where number = ?";
        List<Student> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Student.class), number);
        return list.isEmpty() ? null : list.get(0);
    }

    @Override
    public List<Student> selectAllStudent() {
        String sql = "select * from students";
        List<Student> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Student.class));
        return list.isEmpty() ? null : list;
    }


}